using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WMS.Api.Application.Commands;
using WMS.Common;

namespace WMS.Api.Controllers
{
    /// <summary>
    /// 箱子型号控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [AllowAnonymous]
    public class BoxTypesController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<BoxTypesController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志</param>
        public BoxTypesController(IMediator mediator, ILogger<BoxTypesController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 获取箱子型号列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="name">型号名称</param>
        /// <param name="status">状态</param>
        /// <returns>箱子型号列表</returns>
        [HttpGet]
        public async Task<ApiResult<PagingResult<List<BoxTypeDto>>>> GetBoxTypeList(
            int pageIndex = 1,
            int pageSize = 10,
            string? name = null,
            int? status = null)
        {
            try
            {
                var command = new GetBoxTypeListCommand
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    Name = name,
                    Status = status
                };

                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取箱子型号列表失败");
                return new ApiResult<PagingResult<List<BoxTypeDto>>>("获取箱子型号列表失败：" + ex.Message);
            }
        }

        /// <summary>
        /// 获取箱子型号详情
        /// </summary>
        /// <param name="id">箱子型号ID</param>
        /// <returns>箱子型号详情</returns>
        [HttpGet]
        public async Task<ApiResult<BoxTypeDto>> GetBoxTypeById(int id)
        {
            try
            {
                var command = new GetBoxTypeByIdCommand { Id = id };
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取箱子型号详情失败");
                return new ApiResult<BoxTypeDto>("获取箱子型号详情失败：" + ex.Message);
            }
        }

        /// <summary>
        /// 创建箱子型号
        /// </summary>
        /// <param name="boxType">箱子型号信息</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateBoxType([FromBody] BoxTypeDto boxType)
        {
            try
            {
                var command = new CreateBoxTypeCommand { BoxType = boxType };
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "创建箱子型号失败");
                return new ApiResult<int>("创建箱子型号失败：" + ex.Message);
            }
        }

        /// <summary>
        /// 更新箱子型号
        /// </summary>
        /// <param name="boxType">箱子型号信息</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdateBoxType([FromBody] BoxTypeDto boxType)
        {
            try
            {
                var command = new UpdateBoxTypeCommand { BoxType = boxType };
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "更新箱子型号失败");
                return new ApiResult<int>("更新箱子型号失败：" + ex.Message);
            }
        }

        /// <summary>
        /// 删除箱子型号
        /// </summary>
        /// <param name="id">箱子型号ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete]
        public async Task<ApiResult<int>> DeleteBoxType(int id)
        {
            try
            {
                var command = new DeleteBoxTypeCommand { Id = id };
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除箱子型号失败");
                return new ApiResult<int>("删除箱子型号失败：" + ex.Message);
            }
        }
    }
} 